package cn.hm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/7/31 10:53
 */

@RestController
@RequestMapping("msg")
public class SendMsgController {

    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private Queue queue;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;


    @RequestMapping("send")
    public String send(String msg) {
        jmsMessagingTemplate.convertAndSend(queue, msg);

        return "success" + Thread.currentThread().getName();
    }


    /**
     * 发送队列
     */
    //@RequestMapping("sendQueue")
    //public Map<String, Object> sendQueue() {
    //    Map<String, Object> map = new HashMap<String, Object>();
    //    jmsTemplate.setDefaultDestination(new ActiveMQQueue("queue-hello"));
    //    jmsTemplate.send(new MessageCreator() {
    //        @Override
    //        public TextMessage createMessage(Session session) throws JMSException {
    //            return session.createTextMessage("hello-queue-msg");
    //        }
    //    });
    //    map.put("success", "发送成功");
    //    return map;
    //}

    /**
     * 发送主题
     */
    //@RequestMapping("sendTopic")
    //public Map<String, Object> sendTopic() {
    //    Map<String, Object> map = new HashMap<String, Object>();
    //    jmsTemplate.setDefaultDestination(new ActiveMQTopic("topic-hello"));
    //    jmsTemplate.send(new MessageCreator() {
    //        @Override
    //        public TextMessage createMessage(Session session) throws JMSException {
    //            return session.createTextMessage("hello-topic-msg");
    //        }
    //    });
    //    map.put("success", "发送成功");
    //    return map;
    //}
}
